package cn.iocoder.yudao.module.peizhen.service.staff;

import cn.iocoder.yudao.module.peizhen.controller.app.staff.vo.AppStaffPageReqVO;
import cn.iocoder.yudao.module.peizhen.controller.app.staff.vo.AppStaffSaveReqVO;
import cn.iocoder.yudao.module.peizhen.dal.dataobject.opencity.OpenCityDO;
import cn.iocoder.yudao.module.peizhen.service.opencity.OpenCityService;
import cn.iocoder.yudao.untils.OrderNumUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import cn.iocoder.yudao.module.peizhen.controller.admin.staff.vo.*;
import cn.iocoder.yudao.module.peizhen.dal.dataobject.staff.StaffDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.peizhen.dal.mysql.staff.StaffMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.peizhen.enums.ErrorCodeConstants.*;

/**
 * 陪诊师、护工 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class StaffServiceImpl implements StaffService {

    @Resource
    private StaffMapper staffMapper;

    @Resource
    private OpenCityService openCityService;

    @Override
    public Integer createStaff(StaffSaveReqVO createReqVO) {
        // 插入
        //查早省市
        OpenCityDO openCity = openCityService.getOpenCity(createReqVO.getCityCode());
        OpenCityDO province = openCityService.getOpenCity(openCity.getParentId());
        createReqVO.setNo("NO"+OrderNumUtil.generateInvoiceNumber());
        createReqVO.setCity(openCity.getName());
        createReqVO.setProvinceCode(province.getId());
        createReqVO.setProvince(province.getName());
        StaffDO staff = BeanUtils.toBean(createReqVO, StaffDO.class);
        staffMapper.insert(staff);
        // 返回
        return staff.getId();
    }

    @Override
    public void updateStaff(StaffSaveReqVO updateReqVO) {
        // 校验存在
        validateStaffExists(updateReqVO.getId());
        // 更新
        OpenCityDO openCity = openCityService.getOpenCity(updateReqVO.getCityCode());
        OpenCityDO province = openCityService.getOpenCity(openCity.getParentId());
        updateReqVO.setCity(openCity.getName());
        updateReqVO.setProvinceCode(province.getId());
        updateReqVO.setProvince(province.getName());
        StaffDO updateObj = BeanUtils.toBean(updateReqVO, StaffDO.class);
        staffMapper.updateById(updateObj);
    }






    @Override
    public void deleteStaff(Integer id) {
        // 校验存在
        validateStaffExists(id);
        // 删除
        staffMapper.deleteById(id);
    }

    private void validateStaffExists(Integer id) {
        if (staffMapper.selectById(id) == null) {
            throw exception(STAFF_NOT_EXISTS);
        }
    }

    @Override
    public StaffDO getStaff(Long id) {
        return staffMapper.selectById(id);
    }

    @Override
    public PageResult<StaffDO> getStaffPage(StaffPageReqVO pageReqVO) {
        return staffMapper.selectPage(pageReqVO);
    }


    //app
    @Override
    public Integer createStaff(AppStaffSaveReqVO createReqVO) {
        // 插入
        //查看是否有重复
        // 校验存在
        StaffDO staffDO = staffMapper.selectOne(StaffDO::getPhone, createReqVO.getPhone());
        if (staffDO!=null){
            throw exception(STAFF_EXISTS);
        }
        OpenCityDO openCity = openCityService.getOpenCity(createReqVO.getCityCode());
        OpenCityDO province = openCityService.getOpenCity(openCity.getParentId());
        createReqVO.setCity(openCity.getName());
        createReqVO.setProvinceCode(province.getId());
        createReqVO.setProvince(province.getName());
        StaffDO staff = BeanUtils.toBean(createReqVO, StaffDO.class);
        staffMapper.insert(staff);
        // 返回
        return staff.getId();
    }

    @Override
    public void updateStaff(AppStaffSaveReqVO updateReqVO) {
        // 校验存在
        validateStaffExists(updateReqVO.getId());
        // 更新
        StaffDO updateObj = BeanUtils.toBean(updateReqVO, StaffDO.class);
        staffMapper.updateById(updateObj);
    }



    @Override
    public PageResult<StaffDO> getStaffPage(AppStaffPageReqVO pageReqVO) {
        return staffMapper.selectPage(pageReqVO);
    }


    @Override
    public StaffDO getStaffByMobile(String mobile) {
        StaffDO staffDO = staffMapper.selectOne(StaffDO::getPhone,mobile,StaffDO::getIsAudit,1);
        return staffDO;
    }

    @Override
    public void updateStaffOnlie(AppStaffSaveReqVO updateReqVO) {
        StaffDO staffDO = staffMapper.selectById(updateReqVO.getId());
        // 更新
        staffDO.setStatus(updateReqVO.getStatus());
        staffMapper.updateById(staffDO);
    }
}